package models

import (
	"github.com/astaxie/beego/orm"
	"fmt"
	"strconv"
)

type FileInfo struct {
	Id int64
	Filename string
	Filetype string
	Filepath  string
	Deviceid string
	Company  string
	Timestamp int64
	Uploadtime string
}

//加载所有文件
func ReadFile(fileDataChan chan string,company string,page string, size string) int {
	var filerows []FileInfo
	o := orm.NewOrm()
	pageint, _ := strconv.ParseInt(page, 10, 64)
	sizeint, _ := strconv.ParseInt(size, 10, 64)
	var totalItem int = 0
	o.Raw("SELECT COUNT(*) FROM file_info where company = ? ", company).QueryRow(&totalItem)
	//fmt.Println(totalItem)
	if _, err := o.Raw("SELECT * FROM file_info where company = ?  ORDER BY uploadtime DESC limit ?,? ", company, (pageint-1)*sizeint, sizeint).QueryRows(&filerows); err != nil {
		fmt.Println("err")
		return 0
	}

	var  i int64= (pageint-1)*sizeint;
	for _,filerow := range filerows{
		i++
		id := strconv.FormatInt(i,10)
		fileInfos :=id+ "and"+ filerow.Filename+
			"and"+filerow.Filetype+
			//"and"+fencerow.Fencepoint+
			"and"+filerow.Deviceid+
			"and"+filerow.Company+
			"and"+filerow.Uploadtime
		fileDataChan <- fileInfos
	}
	return totalItem
}




//添加文件
func AddFile(file_info *FileInfo)(int64,error){
	db := orm.NewOrm()
	id,err := db.Insert(file_info)
	return id,err
}